/*
 * @Description: 路由文件  导航守卫
 * @Author: YWJ 
 * @Date: 2022-04-20 14:25:39 
 * @Last Modified by: YWJ
 * @Last Modified time: 2022-05-15 23:22:19
 */

import Vue from 'vue'
import Router from 'vue-router'
import Home from "@/views/Home"
import Body from "@/views/Body"
import CateGory from '@/views/category'
import BlogTag from '@/views/Blog/BlogTag.vue'
import BlogView from '@/views/Blog/BlogViewTest.vue'
import Register from '@/views/Register/register.vue'
import Login from '@/views/Login/login.vue';
import writeBlog from '@/views/Write/writeBlog.vue'
import editBlog from '@/views/Write/editBlog.vue'
import personalCenter from '@/views/PersonalCenter'
import personalCenterData from '@/views/PersonalCenter/data.vue'
import personalArticle from '@/views/PersonalCenter/personArticle.vue'
import focusAuthor from '@/views/PersonalCenter/focusAuthor.vue'
import focusArticle from '@/views/PersonalCenter/focusArticle.vue'
import collectArticle from '@/views/PersonalCenter/collectArticle.vue'
import likeArticle from '@/views/PersonalCenter/likeArticle.vue'
import searchBlog from '@/views/search'
import store from '@/store'
import{Message} from 'element-ui'
Vue.use(Router)

const VueRouterPush = Router.prototype.push
Router.prototype.push = function push(to) {
    return VueRouterPush.call(this, to).catch(err => err)
}


const router = new Router({
    routes: [

        {
            path: '',
            component: Home,
            children: [
                {
                    path: '/',
                    component: Body
                },
                {
                    path: '/tag/all',
                    component: CateGory
                },
                {
                    path: '/tag/:id',
                    component: BlogTag
                },
                {
                    path: '/view/:id',
                    component: BlogView
                },
                {
                    path: '/write',
                    component: writeBlog
                },
                {
                    path: '/search',
                    component: searchBlog
                },
                {
                    path: '/editBlog/:id',
                    component: editBlog
                },
                {
                    path: '/personalcenter',
                    component: personalCenter,
                    redirect:'/personalcenter/data',
                    children:[
                        {
                            path:'/personalcenter/data',
                            component:personalCenterData
                        },
                        {
                            path:'/personalcenter/article',
                            component:personalArticle
                        },
                        {
                            path:'/personalcenter/collect',
                            component:collectArticle
                        },
                        {
                            path:'/personalcenter/like',
                            component:likeArticle
                        },
                        {
                            path:'/personalcenter/focusAuthor',
                            component:focusAuthor
                        },
                        {
                            path:'/personalcenter/focusArticle',
                            component:focusArticle
                        },
                    ]
                }
            ]
        },
        {
            path: '/register',
            component: Register
        },
        {
            path: '/login',
            component: Login
        },

    ]
})

// router.beforeEach((to, from, next) => {
//     if (store.state.token) {
//         //路径如果是请求的login登陆页面 如果存在token 直接跳回首页
//         if (to.path == "/login") {
//             next({ path: '/' })
//         } else {

//             //如果存在token 证明用户登陆过
//             //是否有账号信息 如果没有 则获取用户信息
//             if (store.state.account.length == 0) {
//                 //需要获取用户信息
//                 store.dispatch("getUserInfo").then((res) => {
//                     next();
//                 }).catch((error) => {
//                     Message({
//                         type: 'warning',
//                         showClose: true,
//                         message: '登录已过期'
//                     })
//                     next({ path: '/' })

//                 })
//             } else {
//                 next();
//             }
//         }
//     }else {
//         if (to.matched.some(r => r.meta.requireLogin)) {
//           Message({
//             type: 'warning',
//             showClose: true,
//             message: '请先登录哦'
//           })
    
//         }
//         else {
//           next();
//         }
//       }
// })
export default router



